package com.ruoyi.cmecomon.service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cmecomon.domain.SysNoticeCme;
import com.ruoyi.cmecomon.domain.ZxOrderPostman;
import com.ruoyi.cmelasercladding.domain.ZsCladdingProcess;
import com.ruoyi.cmelasercladding.domain.ZxLaserCladdingOrderForm;
import com.ruoyi.cmelasercladding.service.IZsCladdingProcessService;
import com.ruoyi.cmelasercladding.service.IZxLaserCladdingOrderFormService;
import com.ruoyi.cmemetal.domain.*;
import com.ruoyi.cmemetal.service.*;
import com.ruoyi.cmenonmetal.domain.ZsDataPreprocess;
import com.ruoyi.cmenonmetal.domain.ZsPostProcess;
import com.ruoyi.cmenonmetal.domain.ZsPrintProcess;
import com.ruoyi.cmenonmetal.domain.ZxNonmetalOrderForm;
import com.ruoyi.cmenonmetal.service.IZsDataPreprocessService;
import com.ruoyi.cmenonmetal.service.IZsPostProcessService;
import com.ruoyi.cmenonmetal.service.IZsPrintProcessService;
import com.ruoyi.cmenonmetal.service.IZxNonmetalOrderFormService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HomePageServiceImpl implements HomePageService{

    @Autowired
    private ISysNoticeCmeService sysNoticeCmeService;

    @Autowired
    private IZxOrderFormService zxOrderFormService;

    @Autowired
    private IZxNonmetalOrderFormService zxNonmetalOrderFormService;

    @Autowired
    private IZxLaserCladdingOrderFormService zxLaserCladdingOrderFormService;

    @Autowired
    private IZxPostProcessService zxPostProcessService;

    @Autowired
    private IZsPostProcessService zsPostProcessService;

    @Autowired
    private IZsCladdingProcessService zsCladdingProcessService;

    @Autowired
    private IZxDataPreprocessService zxDataPreprocessService;

    @Autowired
    private IZsDataPreprocessService zsDataPreprocessService;

    @Autowired
    private IZxPrintProcessService zxPrintProcessService;

    @Autowired
    private IZxWireCutService zxWireCutService;

    @Autowired
    private IZxHeatPreprocessService zxHeatPreprocessService;

    @Autowired
    private IZsPrintProcessService zsPrintProcessService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;




    @Override
    public List<SysNoticeCme> getCmeSysNotice() {

        SysNoticeCme sysNotice = new SysNoticeCme();
        List<SysNoticeCme> sysNotices = sysNoticeCmeService.selectSysNoticeCmeList(sysNotice);
        List<SysNoticeCme> sysNoticeCmes = new ArrayList<>();
        if(sysNotices.size() != 0){
            for (SysNoticeCme notice : sysNotices) {
                if(notice.getStatus().equals("0")){
                    sysNoticeCmes.add(notice);
                }
            }
        }

        return sysNoticeCmes;
    }

    @Override
    //获取订单总量，交货量，完成率
    public AjaxResult getOrders() {

        //order统计
        ZxOrderForm metalOrder = new ZxOrderForm();
        ZxLaserCladdingOrderForm lasOrder = new ZxLaserCladdingOrderForm();
        ZxNonmetalOrderForm  nometalOrder = new ZxNonmetalOrderForm();
        List<ZxOrderForm> zxOrderForms = zxOrderFormService.selectZxOrderFormList(metalOrder);
        List<ZxLaserCladdingOrderForm> zxLaserCladdingOrderForms = zxLaserCladdingOrderFormService.selectZxLaserCladdingOrderFormList(lasOrder);
        List<ZxNonmetalOrderForm> zxNonmetalOrderForms = zxNonmetalOrderFormService.selectZxNonmetalOrderFormList(nometalOrder);
        int orderTotal = zxOrderForms.size() + zxLaserCladdingOrderForms.size() + zxNonmetalOrderForms.size();


        //完成量统计
        ZxPostProcess zxPostProcess = new ZxPostProcess();
        ZsPostProcess zsPostProcess = new ZsPostProcess();
        ZsCladdingProcess zsCladdingProcess = new ZsCladdingProcess();
        List<ZxPostProcess> zxPostProcesses = zxPostProcessService.selectZxPostProcessList(zxPostProcess);
        List<ZsPostProcess> zsPostProcesses = zsPostProcessService.selectZsPostProcessList(zsPostProcess);
        List<ZsCladdingProcess> zsCladdingProcesses = zsCladdingProcessService.selectZsCladdingProcessList(zsCladdingProcess);
        int completeQuantity =0;
        for (ZxPostProcess postProcess : zxPostProcesses) {
            if(postProcess.getIsHandl() == 1){
                completeQuantity++;
            }
        }
        for (ZsPostProcess postProcess : zsPostProcesses) {
            if(postProcess.getIsHandl()==1){
                completeQuantity++;
            }
        }
        for (ZsCladdingProcess claddingProcess : zsCladdingProcesses) {
            if(claddingProcess.getIsHandle() == 1){
                completeQuantity++;
            }
        }

        //构造完成率
        double i = ((double) completeQuantity / (double) orderTotal)*100;
        DecimalFormat decimalFormat= new DecimalFormat( ".00" );
        String completeRate = decimalFormat.format(i);

        AjaxResult success = AjaxResult.success();
        success.put("orderTotal",orderTotal);
        success.put("completeQuantity",completeQuantity);
        success.put("completeRate",completeRate);
        return success;
    }

    @Override
    //获取本周各产品量
    public AjaxResult getWeekOrder() {
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");

        //获取当前时间
        Date dateNow = new Date();
        Date dateBefore = new Date();

        //获取日历用于计算前六天日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateNow);

        //为前端准备相应数据集合
        List<String>   dates            = new ArrayList<>();
        List<Integer>  metalOrders     = new ArrayList<>();
        List<Integer>  nometalOrders   = new ArrayList<>();
        List<Integer>  laserCladdings  = new ArrayList<>();

        List<ZxOrderForm> zxOrderForms = null;
        List<ZxNonmetalOrderForm> zxNonmetalOrderForms = null;
        List<ZxLaserCladdingOrderForm> zxLaserCladdingOrderForms = null;
        cal.add(Calendar.DAY_OF_MONTH, -6);
        for(int i = 0;i<=6;i++){
            if(i == 0){
                cal.add(Calendar.DAY_OF_MONTH, 0);
            }else {
                cal.add(Calendar.DAY_OF_MONTH, +1);
            }

            dateBefore = cal.getTime();
            zxOrderForms = zxOrderFormService.selectZxOrderBydate(dateBefore);
            zxNonmetalOrderForms = zxNonmetalOrderFormService.selectNonmetalOrderBydate(dateBefore);
            zxLaserCladdingOrderForms = zxLaserCladdingOrderFormService.selectZxLaserCladdingOrderFormBydate(dateBefore);

            if(zxOrderForms!=null && zxNonmetalOrderForms!=null && zxLaserCladdingOrderForms!=null){
                metalOrders.add(zxOrderForms.size());
                nometalOrders.add(zxNonmetalOrderForms.size());
                laserCladdings.add(zxLaserCladdingOrderForms.size());
            }else {
                metalOrders.add(0);
                nometalOrders.add(0);
                laserCladdings.add(0);
            }

            String beforDate = sdf.format(dateBefore);
            dates.add(beforDate);
        }

        AjaxResult success = AjaxResult.success();
        success.put("dates",dates);
        success.put("metalOrders",metalOrders);
        success.put("nometalOrders",nometalOrders);
        success.put("laserCladdings",laserCladdings);

        return success;
    }

    @Override
    public AjaxResult getQroductquality() {

        //合格
        int   qualified = 0;
        //外包
        int   outsource = 0;
        //报废
        int   scrap     = 0;


        ZxDataPreprocess zxDataPreprocess = new ZxDataPreprocess();
        ZsDataPreprocess zsDataPreprocess = new ZsDataPreprocess();
        ZxLaserCladdingOrderForm lasOrder = new ZxLaserCladdingOrderForm();

        ZxPrintProcess zxPrintProcess = new ZxPrintProcess();
        ZxWireCut zxWireCut = new ZxWireCut();
        ZxHeatPreprocess zxHeatPreprocess = new ZxHeatPreprocess();
        ZxPostProcess zxPostProcess = new ZxPostProcess();


        ZsPrintProcess zsPrintProcess =new ZsPrintProcess();
        ZsPostProcess zsPostProcess =new ZsPostProcess();

        List<ZxDataPreprocess> zxDataPreprocesses = zxDataPreprocessService.selectZxDataPreprocessList(zxDataPreprocess);
        List<ZsDataPreprocess> zsDataPreprocesses = zsDataPreprocessService.selectZsDataPreprocessList(zsDataPreprocess);
        List<ZxLaserCladdingOrderForm> zxLaserCladdingOrderForms = zxLaserCladdingOrderFormService.selectZxLaserCladdingOrderFormList(lasOrder);


        int orderTotal =  zxLaserCladdingOrderForms.size() + zsDataPreprocesses.size() + zxDataPreprocesses.size();

        for (ZxDataPreprocess dataPreprocess : zxDataPreprocesses) {
            if(dataPreprocess.getResult() == 2 || dataPreprocess.getResult() == 0){
                outsource++;
            }
        }

        for (ZsDataPreprocess dataPreprocess : zsDataPreprocesses) {
            if(dataPreprocess.getResult() == 2 || dataPreprocess.getResult() ==0){
                outsource++;
            }
        }


        List<ZxPrintProcess> zxPrintProcesses = zxPrintProcessService.selectZxPrintProcessList(zxPrintProcess);
        List<ZxWireCut> zxWireCuts = zxWireCutService.selectZxWireCutList(zxWireCut);
        List<ZxHeatPreprocess> zxHeatPreprocesses = zxHeatPreprocessService.selectZxHeatPreprocessList(zxHeatPreprocess);
        List<ZxPostProcess> zxPostProcesses = zxPostProcessService.selectZxPostProcessList(zxPostProcess);

        List<ZsPrintProcess> zsPrintProcesses = zsPrintProcessService.selectZsPrintProcessList(zsPrintProcess);
        List<ZsPostProcess> zsPostProcesses = zsPostProcessService.selectZsPostProcessList(zsPostProcess);

        for (ZxPrintProcess printProcess : zxPrintProcesses) {
            if(printProcess.getIsqualified() ==2){
                scrap++;
            }
        }

        for (ZxWireCut wireCut : zxWireCuts) {
            if(wireCut.getIsqualified() == 2){
                scrap++;
            }
        }

        for (ZxHeatPreprocess heatPreprocess : zxHeatPreprocesses) {
            if(heatPreprocess.getIsqualified() == 2){
                scrap++;
            }
        }

        for (ZxPostProcess postProcess : zxPostProcesses) {
            if(postProcess.getIsqualified() == 2){
                scrap++;
            }
        }

        for (ZsPrintProcess printProcess : zsPrintProcesses) {
            if(printProcess.getIsqualitified() == 2){
                scrap++;
            }
        }

        for (ZsPostProcess postProcess : zsPostProcesses) {
            if(postProcess.getIsqualified() == 2){
                scrap++;
            }
        }

        qualified = orderTotal - scrap - outsource;

        AjaxResult success = AjaxResult.success();
        success.put("qualified",qualified);
        success.put("scrap",scrap);
        success.put("outsource",outsource);

        return success;
    }

    @Override
    public AjaxResult getclassificationStatistics() {

        int metalOrders     = 0;
        int noMetaalOrders  = 0;
        int laserOrders    = 0;


        HashMap<String,String> noMetalMap = new HashMap<>();
        HashMap<String,Integer> noMetalClass = new HashMap<>();

        HashMap<String,String> metalMap = new HashMap<>();
        HashMap<String,Integer> metalClass = new HashMap<String,Integer>();

        ZxPrintProcess zxPrintProcess = new ZxPrintProcess();
        List<ZxPrintProcess> zxPrintProcesses = zxPrintProcessService.selectZxPrintProcessList(zxPrintProcess);

        List<SysDictData> sys_cme_metalPrintitem = sysDictTypeService.selectDictDataByType("sys_cme_metalPrintitem");

        metalOrders = zxPrintProcesses.size();

        for (SysDictData sysDictData : sys_cme_metalPrintitem) {
            metalMap.put(sysDictData.getDictValue(),sysDictData.getDictLabel());
            metalClass.put(sysDictData.getDictLabel(),0);
        }

        String lale1 = null;
        List<Object> metalClassList = new ArrayList<>();
        for (ZxPrintProcess printProcess : zxPrintProcesses) {
            lale1 =  metalMap.get(printProcess.getPrintItem());
            if(lale1 != null){
                metalClass.put(lale1,metalClass.get(lale1) + 1);
                List<Object> metal = new ArrayList<>();
                metal.add(lale1);
                metal.add(metalClass.get(lale1));
                metalClassList.add(metal);
            }
        }


        ZsPrintProcess zsPrintProcess =new ZsPrintProcess();
        List<ZsPrintProcess> zsPrintProcesses = zsPrintProcessService.selectZsPrintProcessList(zsPrintProcess);
        noMetaalOrders = zsPrintProcesses.size();

        List<SysDictData> nonmetal_printtype = sysDictTypeService.selectDictDataByType("nonmetal_printtype");
        for (SysDictData sysDictData : nonmetal_printtype) {
            noMetalMap.put(sysDictData.getDictValue(),sysDictData.getDictLabel());
            noMetalClass.put(sysDictData.getDictLabel(),0);
        }
        String lable2 = null;
        List<Object> noMetalClassList = new ArrayList<>();
        for (ZsPrintProcess printProcess : zsPrintProcesses) {
            lable2 = noMetalMap.get(printProcess.getPrintType());
            if(lable2 != null){
                noMetalClass.put(lable2,noMetalClass.get(lable2)+1);
                List<Object>noMetal = new ArrayList<>();
                noMetal.add(lable2);
                noMetal.add(noMetalClass.get(lable2));
                noMetalClassList.add(noMetal);
            }
        }

        ZxLaserCladdingOrderForm zxLaserCladdingOrderForm =new ZxLaserCladdingOrderForm();
        List<ZxLaserCladdingOrderForm> zxLaserCladdingOrderForms = zxLaserCladdingOrderFormService.selectZxLaserCladdingOrderFormList(zxLaserCladdingOrderForm);
        laserOrders = zxLaserCladdingOrderForms.size();

        AjaxResult success = AjaxResult.success();
        success.put("metalOrders"   ,  metalOrders     );
        success.put("noMetaalOrders" , noMetaalOrders  );
        success.put("laserOrders"   ,  laserOrders     );
        success.put("metalClassList" ,     metalClassList      );
        success.put("noMetalClassList",    noMetalClassList     );

        return success;
    }
}
